package cn.mumzone.pos.ui.widgets;


import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.BannerEntity;

/**
 * 副屏
 */

public class DifferentDisplay extends Presentation {

    private Banner banner;

    List<BannerEntity> images;

    boolean isEmpty;

    public DifferentDisplay(Context outerContext, Display display) {
        super(outerContext, display);
        images = new ArrayList<>();
        isEmpty = false;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_diff_display);
        banner = findViewById(R.id.banner);
        banner.isAutoPlay(true);
        banner.setDelayTime(2800);
        banner.setImages(images).setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                BannerEntity bannerEntity = (BannerEntity) path;
                if (isEmpty) {
                    imageView.setImageResource(Integer.parseInt(bannerEntity.getPicUrl()));
                } else {
                    Glide.with(context).load(bannerEntity.getPicUrl()).into(imageView);
                }
            }

            @Override
            public ImageView createImageView(Context context) {
                ImageView imageView = new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });
        banner.start();
    }

    public void setImages(List<BannerEntity> images) {
        isEmpty = ListUtil.isListEmpty(images);
        if (isEmpty) {
            BannerEntity defaultBanner = new BannerEntity();
            defaultBanner.setPicUrl(String.valueOf(R.mipmap.default_banner));
            images.add(defaultBanner);
        }
        this.images = images;
    }

    @Override
    protected void onStart() {
        super.onStart();
        //开始轮播
        banner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //结束轮播
        banner.stopAutoPlay();
    }
}
